<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- 添加根模板匹配 -->
    <xsl:template match="/">
        <xsl:apply-templates select="contacts"/>
    </xsl:template>

    <xsl:template match="contacts">
        <html>
            <head>
                <title>联系人列表</title>
                <style>
                    table { width: 80%; margin: 20px auto; border-collapse: collapse; }
                    th, td { border: 1px solid #333; padding: 10px; }
                    th { background: #f0f0f0; }
                    .male { color: blue; }
                    .female { color: pink; }
                </style>
            </head>
            <body>
                <h2 style="text-align: center;">联系人信息（来自FOR XML查询）</h2>
                <table>
                    <tr>
                        <th>ID</th>
                        <th>姓名</th>
                        <th>性别</th>
                        <th>联系方式</th>
                        <th>标签</th>
                    </tr>
                    <xsl:for-each select="contact">
                        <tr>
                            <td><xsl:value-of select="@id"/></td>
                            <td><xsl:value-of select="name"/></td>
                            <td>
                                <xsl:choose>
                                    <xsl:when test="gender='M'">
                                        <span class="male">男</span>
                                    </xsl:when>
                                    <xsl:when test="gender='F'">
                                        <span class="female">女</span>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <xsl:value-of select="gender"/>
                                    </xsl:otherwise>
                                </xsl:choose>
                            </td>
                            <td>
                                邮箱：<xsl:value-of select="contact/email"/><br/>
                                电话：<xsl:value-of select="contact/phone"/>
                            </td>
                            <td><xsl:value-of select="tags"/></td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>
